home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / BOOZ_MAC / BOOZ_2 / OOZEXT.C < prev    next >
Text File  |  1992-07-19  |  8KB  |  264 lines

  1. /* oozext.c -- extracts files from archive */
  2.  
  3. /* Main extraction module.  This file is public domain.
  4.  
  5.                                    -- Rahul Dhesi 1991/07/07
  6.  
  7. The function fixfname() is currently defined to be empty but may be
  8. activated if needed to fix filename syntax to be legal for your
  9. computer system.  Look near the end of this file for instructions and
  10. a sample implementation of fixfname().  Currently, fixfname() is used
  11. only if the symbol FIXFNAME is defined.
  12. */
  13.  
  14. #include "booz.h"
  15. #include "zoo.h"
  16. #include <stdio.h>
  17.  
  18. #ifdef MAC
  19. #include <string.h>
  20. #endif
  21.  
  22. extern unsigned int crccode;
  23.  
  24. int needed ();
  25.  
  26. int oozext (zoo_path, option, argc, argv)
  27. char *zoo_path;
  28. char *option;
  29. int argc;
  30. char *argv[];
  31.  
  32. {
  33. FILE *zoofile;                            /* open archive */
  34. FILE *this_file;                          /* file to extract */
  35. long next_ptr;                            /* pointer to within archive */
  36. struct zoo_header zoo_header;             /* header for archive */
  37. int status;                               /* error status */
  38. struct direntry direntry;                 /* directory entry */
  39. int all = 0;                              /* overwrite all? */
  40. static char vermsg[] = "A higher version of Ooz is needed to extract ";
  41. int exitstat = 0;                         /* return code */
  42.  
  43. int first_time = 1;
  44. static char *month_list="000JanFebMarAprMayJunJulAugSepOctNovDec";
  45.  
  46. {
  47.    /* If no dot in name, add ".zoo" extension */
  48.    char *p;
  49.    p = zoo_path;
  50.    while (*p != '\0' && *p != '.')
  51.       p++;
  52.    if (*p != '.') {  /* no dot found */
  53.       p = malloc (strlen (zoo_path) + 5);
  54.       if (p == (char *) 0)
  55.          memerr();
  56.       strcpy (p, zoo_path);
  57.       strcat (p, ".zoo");
  58.       zoo_path = p;
  59.    }
  60. }
  61.  
  62. zoofile = OPEN(zoo_path);
  63.  
  64. if (zoofile == NULL)
  65.    prterror ('f', "Could not open ", zoo_path, "\n");
  66.  
  67. /* read header */
  68. rd_zooh (&zoo_header, zoofile);
  69. fseek(zoofile, zoo_header.zoo_start, 0); /* seek to where data begins */
  70.  
  71. while (1) {
  72.    rd_dir (&direntry, zoofile);
  73.  
  74.    if (direntry.lo_tag != LO_TAG || direntry.hi_tag != HI_TAG)
  75.          prterror ('f', "Bad entry in archive\n", 
  76.             ((char *) 0), ((char *) 0));
  77.    if (direntry.next == 0L) {                /* END OF CHAIN */
  78.       break;                                 /* EXIT on end of chain */
  79.    }
  80.    next_ptr = direntry.next;                 /* ptr to next dir entry */
  81.  
  82.       /* See if this file is wanted */
  83.       if (!needed (direntry.fname, argc, argv)) {
  84.          goto loop_again;
  85.       }
  86.  
  87.   /* If list needed, give information and loop again */
  88.    if (*option == 'l') {
  89.       char outstr[80];
  90.       char buf[20];
  91.       int year, month, day, hours, min, sec;
  92.       int size_factor;
  93.       size_factor = cfactor (direntry.org_size, direntry.size_now);
  94.    
  95.       year  =  ((unsigned int) direntry.date >> 9) & 0x7f;
  96.       month =  ((unsigned int) direntry.date >> 5) & 0x0f;
  97.       day   =  direntry.date        & 0x1f;
  98.       
  99.       hours =  ((unsigned int) direntry.time >> 11)& 0x1f;
  100.       min   =  ((unsigned int) direntry.time >> 5) & 0x3f;
  101.       sec   =  ((unsigned int) direntry.time & 0x1f) * 2;
  102.  
  103.       if (first_time) {
  104.          putstr ("Length    CF  Size Now  Date      Time\n");
  105.          putstr ("--------  --- --------  --------- --------\n");
  106.          first_time = 0;
  107.       }
  108.       strcpy (outstr, itoa(' ', direntry.org_size, buf, 9));
  109.       strcat (outstr, itoa(' ',(long) size_factor, buf, 5));
  110.       strcat (outstr, "% ");
  111.       strcat (outstr, itoa(' ',direntry.size_now, buf, 9));
  112.       strcat (outstr, "  ");
  113.       strcat (outstr, itoa(' ',(long) day, buf, 3));
  114.       strcat (outstr, " ");
  115.       strncat (outstr, &month_list[month*3], 3);
  116.       strcat (outstr, " ");
  117.       if (day && month)
  118.          strcat (outstr, itoa(' ',(long) (year+80) % 100, buf, 3));
  119.       else
  120.          strcat (outstr, itoa(' ',0L, buf, 3));
  121.       strcat (outstr, " ");
  122.       strcat (outstr, itoa('0',(long) hours, buf, 3));
  123.       strcat (outstr, ":");
  124.       strcat (outstr, itoa('0',(long) min, buf, 3));
  125.       strcat (outstr, ":");
  126.       strcat (outstr, itoa('0',(long) sec, buf, 3));
  127.       strcat (outstr, "  ");
  128.       strcat (outstr, direntry.fname);
  129.       strcat (outstr, "\n");
  130.       putstr (outstr);
  131.  
  132. #ifdef COMMENT
  133.       printf ("%8lu %3u%% %8lu  %2d %-.3s %02d %02d:%02d:%02d  ",  
  134.                direntry.org_size, 
  135.                size_factor, direntry.size_now, 
  136.                day, &month_list[month*3], 
  137.                (day && month) ?  (year+80) % 100 : 0,
  138.                hours, min, sec);
  139.       printf ("%s\n", direntry.fname);
  140. #endif
  141.  
  142.      goto loop_again;
  143.    }
  144.  
  145.  
  146.    if (direntry.major_ver > 2 ||
  147.          (direntry.major_ver == 2 && direntry.minor_ver > 1)) {
  148.       prterror ('e', vermsg, direntry.fname, "\n");
  149.       goto loop_again;
  150.    }
  151.  
  152. #ifdef FIXFNAME
  153.    /* Make the filename syntax acceptable to the host system */
  154.    fixfname (direntry.fname);
  155. #endif
  156.  
  157.  
  158.    /* See if this file already exists */
  159.  
  160.    if (*option != 't' && !all) {
  161.       this_file = OPEN(direntry.fname);
  162.       if (this_file != NULL) {
  163.          char ans[2];
  164.          char ans2[2];
  165.          fclose(this_file);
  166.    
  167.          do {
  168.             prterror ('m', "Overwrite ", direntry.fname, " (Yes/No/All)? ");
  169.             fread(ans, 1, 1, stdin);
  170.             do {
  171.            fread(ans2, 1, 1, stdin);
  172.             } while (*ans2 != '\n');
  173.          }  while (*ans != 'y' && *ans != 'Y' && 
  174.                    *ans != 'n' && *ans != 'N' &&
  175.                    *ans != 'a' && *ans != 'A');    
  176.    
  177.          if (*ans == 'a' || *ans == 'A')
  178.             all++;
  179.          if (*ans == 'n' || *ans == 'N') {
  180.             prterror ('m', "Skipping ", direntry.fname, "\n");
  181.             goto loop_again;
  182.          }
  183.       }
  184.    }
  185.  
  186.    if (*option == 't')
  187.       this_file = NULL;
  188.    else
  189.       this_file = CREATE(direntry.fname);
  190.  
  191.    if (*option != 't' && this_file == NULL) {
  192.       prterror ('e', "Could not open ", direntry.fname, " for output.\n");
  193.    } else {
  194.       fseek(zoofile, direntry.offset, 0);
  195.       crccode = 0;      /* Initialize CRC before extraction */
  196.       putstr(direntry.fname);
  197.       putstr(" ");
  198.  
  199.       if (direntry.packing_method == 0)
  200.          status = getfile(zoofile, this_file, direntry.size_now);
  201.       else if (direntry.packing_method == 1)
  202.          status = lzd(zoofile, this_file); /* uncompress */
  203.       else if (direntry.packing_method == 2)
  204.          status = lzh_decode(zoofile, this_file); /* uncompress */
  205.       else
  206.          prterror ('e', vermsg, direntry.fname, "\n");
  207.       if (status != 0) {
  208.          unlink(direntry.fname);
  209.          if (status == 1) {
  210.             memerr();
  211.          } else 
  212.             prterror ('e', "I/O error writing ", direntry.fname, "\n");
  213.       } else {
  214.          if (direntry.file_crc != crccode) {
  215.             putstr ("<--\007WARNING:  Bad CRC.\n");
  216.             exitstat = 1;
  217.          } else {
  218.             putstr ("\n");
  219.      }
  220.       } /* end if */
  221.    } /* end if */
  222.  
  223.    if (*option != 't')
  224.       fclose(this_file);
  225.  
  226. loop_again:
  227.    fseek(zoofile, next_ptr, 0); /* ..seek to next dir entry */
  228. } /* end while */
  229. fclose(zoofile);
  230. exit (exitstat);
  231. } /* end oozext */
  232.  
  233. /*
  234. Function fixfname() fixes the syntax of the supplied filename so it is
  235. acceptable to the host system.  We call the standard string function 
  236. strchr(s, c) which searches a string s for a character c and returns
  237. a pointer to it or returns NULL if not found.  The scheme below
  238. strips the 8th bit and changes control characters to corresponding 
  239. printable characters (e.g.  ^A becomes A).
  240.  
  241. As supplied, fixfname() is activated only if the symbol FIXFNAME
  242. is defined.
  243. */
  244.  
  245.  
  246. #ifdef FIXFNAME
  247. /* set of all characters legal for our system */
  248. char legal[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.";
  249.  
  250. char *strchr ();
  251.  
  252. int fixfname (fname)
  253. char *fname;
  254. {
  255.    char *p;
  256.    for (p = fname;  *p != '\0';  p++) {
  257.       *p &= 0x7f;
  258.       if (strchr (legal, *p) == (char *) 0) {
  259.          *p = legal[(*p) % 26];
  260.       }
  261.    }
  262. }
  263. #endif /* FIXFNAME */
  264.